<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Model\Admin\Admin;
use App\Model\Device;
use App\Model\DevicePort;
use App\Model\DistributorDevice;
use App\Model\Distributors;
use App\Model\Order;
use App\Model\Station;
use Hyperf\Redis\Redis;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;

/**
 * 离线管理
 */
class OfflineController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected Device $device;

    #[Inject]
    protected Distributors $distributors;

    #[Inject]
    protected Admin $admin;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**设备列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface|\RedisException
     */
    public function getList(RequestInterface $request): ResponseInterface
    {
        $page = $request->inputs(['page', 'limit', 'station_id', 'distributor_id', 'is_active', 'sn', 'channel', 'is_bind']);
        $jwt_data = $this->admin->getInfo($request);
        $page['info'] = $this->admin->find($jwt_data['id'], ['roles_id', 'station_class']);

        $model = Db::table('devices as d')
            ->leftJoin('stations as s', 'd.station_id', '=', 's.id')
//            ->leftJoin('charge_options as c', 'd.charge_id', '=', 'c.id')
            ->where(function ($query) use ($page) {
                $query->where('d.inline', Device::NOT_ONLINE);
                if ($page['sn']) {
                    $query->where('d.sn', $page['sn']);
                }
                if (isset($page['distributor_id']) && $page['distributor_id']) {
                    $level = Distributors::where('id', $page['distributor_id'])
                        ->value('level');
                    if ($level == Distributors::LEVEL_MAX) {
                        $query->where('d.child_distributor_id', $page['distributor_id']);
                    } else {
                        $query->where('d.distributor_id', $page['distributor_id']);
                    }
                }
                if ($page['channel']) {
                    $query->where('d.channel', $page['channel']);
                }
                if ($page['station_id']) {
                    $query->where('d.station_id', $page['station_id']);
                } else {
                    if ($page['info']['roles_id'] > 0) {
                        $stations = Station::whereIn('class_id', $page['info']['station_class'])->pluck('id')->toArray();
                        $query->whereIn('d.station_id', $stations);
                    }
                }
            })
            ->orderBy('d.id', 'desc')
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->select('d.*', 's.name', 's.address');
        $data['total'] = Db::table('devices as d')
            ->leftJoin('stations as s', 'd.station_id', '=', 's.id')
//            ->leftJoin('charge_options as c', 'd.charge_id', '=', 'c.id')
            ->where(function ($query) use ($page) {
                $query->where('d.inline', Device::NOT_ONLINE);
                if ($page['sn']) {
                    $query->where('d.sn', $page['sn']);
                }
                if (isset($page['distributor_id']) && $page['distributor_id']) {
                    $level = Distributors::where('id', $page['distributor_id'])
                        ->value('level');
                    if ($level == Distributors::LEVEL_MAX) {
                        $query->where('d.child_distributor_id', $page['distributor_id']);
                    } else {
                        $query->where('d.distributor_id', $page['distributor_id']);
                    }
                }
                if ($page['channel']) {
                    $query->where('d.channel', $page['channel']);
                }
                if ($page['station_id']) {
                    $query->where('d.station_id', $page['station_id']);
                } else {
                    if ($page['info']['roles_id'] > 0) {
                        $stations = Station::whereIn('class_id', $page['info']['station_class'])->pluck('id')->toArray();
                        $query->whereIn('d.station_id', $stations);
                    }
                }
            })->count();
        $data['list'] = $model->get()?->toArray();
        return $this->success($data);
    }
}
